using Cinemachine;
using Main.Event;
using UnityEngine;
using ZSW.Framework;

namespace Main.Utils
{
    public class SwitchBounds : ZSWF_InitMonoBehaviour
    {
        [SerializeField] private CinemachineConfiner confiner;

        public override void Init()
        {
            base.Init();
            MainEventSystem.AfterLoadSceneEvent += SwitchConfinerShape;
        }

        private void OnDisable()
        {
            MainEventSystem.AfterLoadSceneEvent -= SwitchConfinerShape;
        }

        public void SwitchConfinerShape()
        {
            Collider2D shape = FindObjectOfType<PolygonCollider2D>(true);

            if (shape == null || confiner == null)
            {
                LogError($"shpae state: {shape == null} confiner state: {confiner == null}");
                return;
            }

            confiner.m_BoundingShape2D = shape;

            //如果边界形状的点在运行时更改，则调用此
            confiner.InvalidatePathCache();

        }

    }
}